package com.tulele.vtool.handler;

import com.google.common.collect.Maps;
import com.tulele.vtool.model.AdvertInfo;
import com.tulele.vtool.model.AdvertTagInfo;
import com.tulele.vtool.model.ArticleAdvertInfo;
import com.tulele.vtool.model.respEntity.RespData;
import com.tulele.vtool.model.respEntity.ReturnCode;
import com.tulele.vtool.service.AdvertInfoService;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class AdvertInfoHandler {

    @Autowired
    private AdvertInfoService advertInfoService;

    /**
     * 20200507 yl
     * 新增广告
     * @param userId
     * @param advertInfo
     * @return
     */
    @PostMapping(value = "/addAdvertByUserId", produces = "application/json;charset=UTF-8")
    public RespData addAdvertByUserId(@RequestHeader("userId") String userId, @RequestBody AdvertInfo advertInfo) {
        if (null == advertInfo || StringUtils.isBlank(userId)){
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        advertInfo.setUserId(Integer.parseInt(userId));
        this.advertInfoService.addAdvertByUserId(advertInfo);
        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20200507 yl
     * 通过userId、广告类型查询广告
     * @param userId
     * @param advertInfo
     * @return
     */
    @PostMapping(value = "/selectAdvertByUserIdAndAdvertType", produces = "application/json;charset=UTF-8")
    public RespData selectAdvertByUserIdAndAdvertType(@RequestHeader("userId") String userId, @RequestBody AdvertInfo advertInfo){
        RespData respData = new RespData();
        if (StringUtils.isBlank(userId)){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        advertInfo.setUserId(Integer.valueOf(userId));
        List<AdvertInfo> advertInfos = this.advertInfoService.selectAdvertByUserIdAndAdvertType(advertInfo);
        if (CollectionUtils.isEmpty(advertInfos)){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("暂无数据");
            return respData;
        }
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(advertInfos);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20200511 yl
     * 广告id查询广告
     * @param userId
     * @param advertInfo
     * @return
     */
    @PostMapping(value = "/selectAdvertByAdvertId", produces = "application/json;charset=UTF-8")
    public RespData selectAdvertByAdvertId(@RequestHeader("userId") String userId, @RequestBody AdvertInfo advertInfo){
        RespData respData = new RespData();
        if (StringUtils.isBlank(userId)){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        advertInfo.setUserId(Integer.valueOf(userId));
        AdvertInfo advertInfos = this.advertInfoService.selectAdvertByAdvertId(advertInfo);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(advertInfos);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20200509 yl
     * 通过广告标题模糊查询广告
     * @param userId
     * @param advertInfo
     * @return
     */
    @PostMapping(value = "/selectAdvertByLikeAdvert", produces = "application/json;charset=UTF-8")
    public RespData selectAdvertByLikeAdvert(@RequestHeader("userId") String userId, @RequestBody AdvertInfo advertInfo){
        RespData respData = new RespData();
        if (StringUtils.isBlank(userId)){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        advertInfo.setUserId(Integer.valueOf(userId));
        List<AdvertInfo> advertInfos = this.advertInfoService.selectAdvertByLikeAdvert(advertInfo);
        if (CollectionUtils.isEmpty(advertInfos)){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("暂无数据");
            return respData;
        }
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setData(advertInfos);
        respData.setMsg("成功");
        return respData;
    }
    /**
     * 20200507 yl
     * 通过广告id删除广告
     * @param userId
     * @param advertInfo
     * @return
     */
    @PostMapping(value = "/deleteAdvertByAdvertId", produces = "application/json;charset=UTF-8")
    public RespData deleteAdvertByAdvertId(@RequestHeader("userId") String userId, @RequestBody AdvertInfo advertInfo){
        RespData respData = new RespData();
        if (null == advertInfo || StringUtils.isBlank(userId)  || null == advertInfo.getId()){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        this.advertInfoService.deleteAdvertByAdvertId(advertInfo.getId());
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20200508 yl
     * 修改广告
     * @param userId
     * @param advertInfo
     * @return
     */
    @PostMapping(value = "/updateAdvertByAdvertId", produces = "application/json;charset=UTF-8")
    public RespData updateAdvertByAdvertId(@RequestHeader("userId") String userId, @RequestBody AdvertInfo advertInfo) {
        if (null == advertInfo || StringUtils.isBlank(userId) || null == advertInfo.getId()){
            RespData respData = new RespData();
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        advertInfo.setUserId(Integer.parseInt(userId));
        this.advertInfoService.updateAdvertByAdvertId(advertInfo);
        RespData respData = new RespData();
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20200508 yl
     * 根据标签类型查询标签或者id查询标签
     * @param userId
     * @param advertTagInfo
     * @return
     */
    @PostMapping(value = "/selectAdvertTagByTagTypeAndTagId", produces = "application/json;charset=UTF-8")
    public RespData selectAdvertTagByTagTypeAndTagId(@RequestHeader("userId") String userId, @RequestBody AdvertTagInfo advertTagInfo){
        RespData respData = new RespData();
        if (null == advertTagInfo){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        List<AdvertTagInfo> advertTagInfos = this.advertInfoService.selectAdvertTagByTagTypeAndTagId(advertTagInfo);
        if (!CollectionUtils.isEmpty(advertTagInfos)) {
            respData.setCode(ReturnCode.NORMAL_SUCC);
            respData.setData(advertTagInfos);
            respData.setMsg("成功");
            return respData;
        }
        respData.setCode(ReturnCode.CLIENT_ERROR);
        respData.setMsg("暂无数据");
        return respData;
    }

    /**
     * 20200508 yl
     * 添加标签
     * @param advertTagInfo
     * @return
     */
    @PostMapping(value = "/addAdvertTag", produces = "application/json;charset=UTF-8")
    public RespData addAdvertTag(@RequestBody AdvertTagInfo advertTagInfo){
        RespData respData = new RespData();
        if (null == advertTagInfo){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        this.advertInfoService.addAdvertTag(advertTagInfo);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;

    }

    /**
     * 20200508 yl
     * 通过标签id删除标签
     * @param advertTagInfo
     * @return
     */
    @PostMapping(value = "/deleteAdvertTagByTagId", produces = "application/json;charset=UTF-8")
    public RespData deleteAdvertTagByTagId(@RequestBody AdvertTagInfo advertTagInfo){
        RespData respData = new RespData();
        if (null == advertTagInfo || null == advertTagInfo.getId()){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        this.advertInfoService.deleteAdvertTagByTagId(advertTagInfo);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20200509 yl
     * 视频添加广告
     * @param articleAdvertInfo
     * @return
     */
    @PostMapping(value = "/addVideoAdvert", produces = "application/json;charset=UTF-8")
    public RespData addVideoAdvert(@RequestHeader("userId") String userId,@RequestBody ArticleAdvertInfo articleAdvertInfo){
        RespData respData = new RespData();
        if (null == articleAdvertInfo || StringUtils.isBlank(userId)){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        articleAdvertInfo.setUserId(Integer.valueOf(userId));
        this.advertInfoService.addVideoAdvert(articleAdvertInfo);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }

    /**
     * 20200509 yl
     * 视频删除广告
     * @param articleAdvertInfo
     * @return
     */
    @PostMapping(value = "/delVideoAdvert", produces = "application/json;charset=UTF-8")
    public RespData delVideoAdvert(@RequestHeader("userId") String userId,@RequestBody ArticleAdvertInfo articleAdvertInfo){
        RespData respData = new RespData();
        if (null == articleAdvertInfo || StringUtils.isBlank(userId)){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        articleAdvertInfo.setUserId(Integer.valueOf(userId));
        this.advertInfoService.delVideoAdvert(articleAdvertInfo);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }
    /**
     * 20200509 yl
     * 通过视频id、广告id查询视频广告
     * @param userId
     * @param
     * @return
     */
    @PostMapping(value = "/selectVideoAdvert", produces = "application/json;charset=UTF-8")
    public RespData selectVideoAdvert(@RequestHeader("userId") String userId, @RequestBody ArticleAdvertInfo articleAdvertInfo){
        RespData respData = new RespData();
        if (null == articleAdvertInfo){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }

        if (articleAdvertInfo.getUserId() != null){
            articleAdvertInfo.setUserId(articleAdvertInfo.getUserId());
        }else{
            articleAdvertInfo.setUserId(Integer.valueOf(userId));
        }

        AdvertInfo advertInfo =this.advertInfoService.selectVideoAdvert(articleAdvertInfo);

        if(advertInfo==null){
            respData.setCode(ReturnCode.ARTICLE_NOT_EXIST);
            respData.setMsg("广告不存在");
            return respData;
        }

        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        respData.setData(advertInfo);
        return respData;
    }

    /**
     * 20200512 yl
     * 视频更换广告
     * @param articleAdvertInfo
     * @return
     */
    @PostMapping(value = "/updateVideoAdvert", produces = "application/json;charset=UTF-8")
    public RespData updateVideoAdvert(@RequestHeader("userId") String userId,@RequestBody ArticleAdvertInfo articleAdvertInfo){
        RespData respData = new RespData();
        if (null == articleAdvertInfo || StringUtils.isBlank(userId)){
            respData.setCode(ReturnCode.CLIENT_ERROR);
            respData.setMsg("参数异常");
            return respData;
        }
        articleAdvertInfo.setUserId(Integer.valueOf(userId));
        this.advertInfoService.updateVideoAdvert(articleAdvertInfo);
        respData.setCode(ReturnCode.NORMAL_SUCC);
        respData.setMsg("成功");
        return respData;
    }







}
